package api

import (
	"smc-admin/controller"
	_ "smc-admin/docs"
	"smc-admin/middleware"
	"smc-admin/response"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	swaggerfiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

func InitRouterGroup() *gin.Engine {
	gin.SetMode(viper.GetString("gin.mode"))
	engine := gin.Default()

	//文件上传目录和文件上传大小
	engine.Static("/upload", "./upload")
	engine.MaxMultipartMemory = 8 << 20

	//全局异常处理中间件
	engine.Use(middleware.Error())

	//404处理
	engine.NoMethod(NoMethod)
	engine.NoRoute(NoRoute)

	//api
	api := engine.Group("/api")
	Client(api)
	User(api)
	Types(api)
	Img(api)
	Video(api)
	Quality(api)

	//上传文件
	api.POST("/upload", controller.SingleFileUpload)

	//swagger
	swag, _ := strconv.ParseBool(viper.GetString("gin.swag"))
	if swag {
		api.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler,
			//ginSwagger.URL("doc.json"),
			ginSwagger.PersistAuthorization(true),
			ginSwagger.DefaultModelsExpandDepth(-1)))
	}

	return engine
}

func NoMethod(ctx *gin.Context) {
	response.Error(ctx, response.Body{
		Msg: "no method",
	})
}

func NoRoute(ctx *gin.Context) {
	response.Error(ctx, response.Body{
		Msg: "no route",
	})
}
